<?php

namespace app\common\model;

use app\beautiful\model\PartnerModel;
use app\common\model\CommonModel;
use app\stores\model\order\BuyOrderModel;
use app\stores\model\order\GoodsOrderModel;
use think\facade\Db;

class MemberConsumeModel extends CommonModel{

    protected  $name = 'member_consume';

    protected  $pk   = 'id';

    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function list($where,$order){
        $query = Db::name('member_consume')->alias('a')
//            ->where(['a.status'=>1])
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['type']),function ($query) use ($where){
                $query->where(['a.type'=>$where['type']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where(['a.start_time'=>$where['start_time']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->field("a.*");
        $list=$query->order($order)
            ->select()
            ->toArray();
// 获取最后一个执行的 SQL 语句
//        $sql = Db::getLastSql();
        return $list;
    }
    public static function ulist($where,$order,$page=1,$limit=10){
        $query = Db::name('member_consume')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->join('beautiful_member m','a.member_id=m.member_id')
//            ->where(['a.status'=>1])
            ->when(isset($where['over_start_time']),function ($query) use ($where){
                $query->where(['a.start_time'=>$where['over_start_time']]);
            })
            ->when(isset($where['over_end_time']),function ($query) use ($where){
                $query->where(['a.end_time'=>$where['over_end_time']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['type']),function ($query) use ($where){
                $query->where(['a.type'=>$where['type']]);
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.end_time','<',$where['end_time']);
            })
            ->when(!empty($where['search']),function ($query) use ($where){
                $query->where('m.nick_name|mobile','like','%'.$where['search'].'%');
            })
            ->when(!empty($where['partake_status']),function ($query) use ($where){
                $query->where(['a.partake_status'=>$where['partake_status']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,m.nick_name,m.mobile,m.face");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $where=[];
                $where['shop_id']=$v['shop_id'];
                $where['type']=$v['type'];
                $where['start_time']=strtotime($v['start_time']);
                $where['end_time']=strtotime($v['end_time']);
                $reward_money=reward_money($where);
                $v['all_reward_money']=$reward_money;
                $o_s=[];
                $o_s['type']=$v['type'];
                $o_s['shop_id']=$v['shop_id'];
                $o_s['start_time']=$v['start_time'];
                $o_s['partake_status']=1;
                $one=self::where($o_s)->order(['sort'=>'desc'])->find();
                $v['cha_money']='--';
                if(!empty($one)){
                    $v['cha_money']=$one['total_money']-$v['total_money'];
                }

                //是否是老客户
                $order_count = GoodsOrderModel::where(['member_id'=>$v['member_id'],'is_paid'=>1])->count();
                $buy_order_count = BuyOrderModel::where(['member_id'=>$v['member_id'],'is_paid'=>1])->count();
                $v['label'] = ($order_count+$buy_order_count)>1?'多次复购，老客户':'新客户';//
                //是否是股东
                $partner = PartnerModel::where(['member_id'=>$v['member_id'],'shop_id'=>$v['shop_id']])->find();
                $v['name'] = $partner?$partner->name:null;
            }
        }
        return compact('count','list');
    }
    //获取消费分红进行中的时间
    public function getTime($where,$field='*'){
        $list = $this->where($where)->field($field)->group(['type'])->select()->toArray();
        return $list;
    }


}